package com.info3k.client.components;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.info3k.client.Info3K;
import com.info3k.client.commands.address.ShowAddressesCommand;
import com.info3k.client.commands.location.ShowLocationCommand;
import com.info3k.client.commands.location.UpdateLocationCommand;
import com.info3k.client.domain.Location;
import com.info3k.client.domain.User;

public class LocationRenderer extends Composite {
	
	private Location location;
	
	public CheckBox selected = new CheckBox();
	
	private UpdateLocationCommand editLocationCommand;
	
	private boolean locationSelected = false;
	
	private VerticalPanel panelV = new VerticalPanel();
	
	private HorizontalPanel panel = new HorizontalPanel();
	
	private Rating rating = new Rating(0, 10, Rating.LTR);
	
	public LocationRenderer(Location loc, boolean selectable, final Info3K info){
		this.setLocation(loc);
		initWidget(panelV);

		if(location.getRate()==null) location.setRate(0.);
		if(location.getViews()==null) location.setViews(0L);
		if(location.getNumRates()==null) location.setNumRates(0L);
		
		panelV.add(panel);
		
		panelV.setWidth("100%");
		panel.setWidth("100%");
		
		VerticalPanel panel1 = new VerticalPanel();
		panel1.add(selected);
		
		HorizontalPanel panel23 = new HorizontalPanel();
		panel23.setWidth("100%");
		
		VerticalPanel panel2 = new VerticalPanel();
		panel2.setWidth("80%");
		
		VerticalPanel panel3 = new VerticalPanel();
		panel3.setWidth("20%");
		
		panel23.add(panel2);
		panel23.add(panel3);
		panel23.setCellHorizontalAlignment(panel3, HorizontalPanel.ALIGN_RIGHT);
		
		final Label locationName = new Label(Info3K.msgs.locationName() + ": " + location.getLocationName());
		final Button showLocation = new Button(Info3K.msgs.showLocation());
		final Button showAddresses = new Button(Info3K.msgs.showAddresses());
		final Button editLocation = new Button(Info3K.msgs.editLocation());
		
		showLocation.getElement().getStyle().setProperty("cursor", "pointer");
		showLocation.getElement().getStyle().setProperty("margin", "3px");
		showLocation.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				ShowLocationCommand showLocationCommand = new ShowLocationCommand(location, info);
				showLocationCommand.execute();
			}
		});
		
		showAddresses.getElement().getStyle().setProperty("cursor", "pointer");
		showAddresses.getElement().getStyle().setProperty("margin", "3px");
		showAddresses.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				ShowAddressesCommand showAddressesCommand = new ShowAddressesCommand(location.getAddresses());
				showAddressesCommand.execute();
			}
		});
		
		editLocation.getElement().getStyle().setProperty("cursor", "pointer");
		editLocation.getElement().getStyle().setProperty("margin", "3px");
		editLocation.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				editLocationCommand = new UpdateLocationCommand(info, location);
				editLocationCommand.execute();
			}
		});
		
		panel2.add(locationName);
		
		if(location.getCategory()!=null){
			final Label locationCategory = new Label(Info3K.msgs.category() + ": " + location.getCategory().getName());
			panel2.add(locationCategory);
		}
		
		if(location.getFloor()!=null){
			final Label locationFloor = new Label(Info3K.msgs.floor() + ": " + location.getFloor());
			panel2.add(locationFloor);
		}
				
		HorizontalPanel hPanel = new HorizontalPanel();
		hPanel.setStylePrimaryName("verClose1");

		User uu = info.getLoginManager().getUser();
		if(uu!=null){
			if(uu.getUsername().equals(location.getUser().getUsername())){
				if(!selectable)
					hPanel.add(editLocation);
			}
		}
		
		if(location.getAddresses().length>0){
			hPanel.add(showAddresses);
		}
		if(location.getLatLong().length>0){
			hPanel.add(showLocation);
		}
		
		if(selectable) {
			panel.add(panel1);
		}
		panel.add(panel23);
		
		String s3 = Info3K.msgs.views() + ": ";
		s3 += "<b>" + location.getViews() + "</b><br>"; 
		
		s3 += Info3K.msgs.rate() + ": ";
		String rate = location.getRate().toString();
		if(rate.length()>4) rate = rate.substring(0, 4);
		s3 += "<b>" + rate + "</b>";
		
		final HTML htmlViewRate = new HTML();
		htmlViewRate.setHTML(s3);
		
		panel3.add(htmlViewRate);
		panel3.add(rating);
		
		panel3.setCellHorizontalAlignment(rating, HorizontalPanel.ALIGN_RIGHT);
		panel3.setCellHorizontalAlignment(htmlViewRate, HorizontalPanel.ALIGN_RIGHT);
		
		rating.setValue(location.getRate().intValue());
		
		if(selectable){
			panel.setCellWidth(panel1, "10%");
			panel.setCellHorizontalAlignment(panel1, HorizontalPanel.ALIGN_CENTER);
			panel.setCellVerticalAlignment(panel1, HorizontalPanel.ALIGN_MIDDLE);
			panel.setCellWidth(panel2, "90%");
		} else {
			panel.setCellWidth(panel2, "100%");
		}
		
		selected.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				if (selected.getValue()) {
					setLocationSelected(true);
					panel.addStyleName("selectedBackground");
				} else {
					setLocationSelected(false);
					panel.removeStyleName("selectedBackground");
				}	
			}
		});
		
		panelV.add(hPanel);
		
		rating.addValueChangeHandler(new ValueChangeHandler<Integer>(){
			public void onValueChange(ValueChangeEvent<Integer> e) {
				Info3K.loader.setVisible(true);
				Info3K.infoService.rateLocation(location, Double.valueOf(e.getValue()), new AsyncCallback<Void>(){
					public void onFailure(Throwable caught) {
						Info3K.loader.setVisible(false);
						InfoWindow.showMessage(Info3K.msgs.error(), caught.getMessage());
					}

					public void onSuccess(Void result) {
						Info3K.loader.setVisible(false);
						InfoWindow.showMessage(Info3K.msgs.ok(), Info3K.msgs.locationRatedSuccessfully());
						Double newRate = ((location.getNumRates() * location.getRate()) + rating.getValue()) / (location.getNumRates()+1);
						location.setNumRates(location.getNumRates() + 1);
						location.setRate(newRate);
						rating.setReadOnly(true);
						String s3 = Info3K.msgs.views() + ": ";
						s3 += "<b>" + location.getViews() + "</b><br>"; 
						
						s3 += Info3K.msgs.rate() + ": ";
						String rate = location.getRate().toString();
						if(rate.length()>4) rate = rate.substring(0, 4);
						s3 += "<b>" + rate + "</b>";
						
						htmlViewRate.setHTML(s3);
					}
				});
			}
		});
	}
	
	public void clickCheckbox(){
		panel.addStyleName("selectedBackground");
		selected.setValue(true);
		setLocationSelected(true);
	}

	public void setLocation(Location location) {
		this.location = location;
	}

	public Location getLocation() {
		return location;
	}

	public void setLocationSelected(boolean locationSelected) {
		this.locationSelected = locationSelected;
	}

	public boolean isLocationSelected() {
		return locationSelected;
	}
}
